<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>旅游信息后台管理系统</title>
    <link rel="icon" href="assets/images/favicon.ico"/>
    <link rel="stylesheet" href="assets/libs/layui/css/layui.css">
    <link rel="stylesheet" href="assets/common.css">
    <script src="assets/libs/jquery/jquery-3.2.1.min.js"></script>
    <script src="assets/libs/jquery/jquery.cookie.js"></script>
</head>

<body>
<div class="login-logo">
    <img src="assets/images/logo.png">
    <cite>旅游信息后台管理系统</cite>
</div>
<div class="login-form">
    <div class="title">用户登录</div>
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <input name="username" type="text" class="layui-input" placeholder="账号" lay-verify="required" required/>
        </div>
        <div class="layui-form-item">
            <input name="password" type="password" class="layui-input" placeholder="密码" lay-verify="required" required/>
        </div>
        <div class="layui-form-item">
            <button class="layui-btn" lay-filter="loginSubmit" lay-submit>&emsp;登录&emsp;</button>
            <button type="reset" class="layui-btn layui-btn-primary">&emsp;重置&emsp;</button>
        </div>
    </form>
</div>

<script type="text/javascript" src="assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="assets/common.js"></script>
<script>
    layui.use(['jquery', 'layer', 'form', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;

        /*if (admin.getToken()) {
            goIndex();
        }*/

        // 监听表单提交
        form.on('submit(loginSubmit)', function (data) {
            console.log(data.field);
            doLogin(data.field);
            return false;
        });

        // 登录
        function doLogin(field) {
            layer.load(2);
            admin.req('/user/login', field, function (res) {
                layer.closeAll('loading');
                if (200 == res.code) {//如果为200 说明为管理员 转到index界面
                    var data=res.data;
                    console.log("username:"+data[0].nickName);
                    console.log("userId:"+data[0].userId);
                    $.cookie('username',data[0].nickName);
                    $.cookie('userId',data[0].userId);
                    layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                        goIndex();
                    });
                }else if(201 == res.code){//code=201代表为 导游登录，转到导游管理index界面
                    var data=res.data;
                    console.log("guidename:"+data[0].guideName);
                    console.log("id:"+data[0].id);
                    $.cookie('guidename',data[0].guideName);
                    $.cookie('id',data[0].id);
                    layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                        goPageIndex();
                    });
                }
                else {
                    layer.msg(res.msg, {icon: 5});
                }
            }, 'POST');
        }

        function goIndex() {
            location.replace('index.html');
        }

        function goPageIndex() {
            location.replace('page/index.html');
        }
    });
</script>
</body>
</html>